package zcw.com.lib_leet_code;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by 朱城委 on 2022-1-15.<br><br>
 *
 * 中等：最长连续序列
 */
public class Topic128 {
    public static void main(String[] args) {
        Topic128 instance = new Topic128();

        int[] array = new int[] {100,4,200,1,3,2};
        System.out.println(instance.longestConsecutive(array));

        array = new int[] {0,3,7,2,5,8,4,6,0,1};
        System.out.println(instance.longestConsecutive(array));
    }

    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for(int value : nums) {
            set.add(value);
        }

        int max = 0;
        for(int value : set) {
            if(!set.contains(value - 1)) {
                int temp = value;
                int counter = 1;

                while (set.contains(++temp)) {
                    counter++;
                }

                max = Math.max(max, counter);
            }
        }

        return max;
    }
} 
